﻿var map;
var point;

function  showmap () {

    
    map = new BMap.Map("MainMap");
    point = new BMap.Point(116.404, 39.915); //参数分别为经度和纬度，以东经北纬为正坐标
    map.centerAndZoom(point, 20); //设置显示级别与填充地图
    map.addControl(new BMap.NavigationControl());
    map.addControl(new BMap.ScaleControl());
    map.addControl(new BMap.OverviewMapControl());
    var marker = new BMap.Marker(point);
    map.addOverlay(marker);
    map.addEventListener("click", function (e) {
        map.removeOverlay(marker);
        marker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));
        map.addOverlay(marker);
        $("#lng").val(e.point.lng);
        $("#lat").val(e.point.lat);
        $("#keyword").val("");

    }
    );

    function SearchBox() {
        this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
        this.defaultOffset = new BMap.Size(0, 0);
    }
    SearchBox.prototype = new BMap.Control();
    SearchBox.prototype.initialize = function (map) {
        var div = document.createElement("div");
        div.style.cursor = "pointer";
        div.style.border = "1px solid gray";
        div.style.backgroundColor = "#3d3d3d";

        div.innerHTML = "<span style='font-family:微软雅黑;font-size:12px;color:#d7d8d9'>经度</span><input id='lng' style='width:100px;border-style:solid'/><span style='font-family:微软雅黑;font-size:12px;color:#d7d8d9';width:100px'>纬度</span><input id='lat' style='width:100px;border-style:solid'/><span style='font-family:微软雅黑;font-size:12px;color:#d7d8d9''>城市</span><input id='keyword' style='width:80px;border-style:solid' /><a id='Lsearch' href='#' style='background-color:#9fc7f6;font-size:12px;font-family:微软雅黑 ;text-decoration:none;color:#3d3d3d'>搜索</a>&nbsp;<a id='okAndCopy' href='#' style='background-color:#9fc7f6;font-size:12px;font-family:微软雅黑 ;text-decoration:none;color:#3d3d3d'>确定</a>";
        map.getContainer().appendChild(div);
        return div;
    }
    var myBox = new SearchBox();

    map.addControl(myBox);

    function myFun() {
        var pp = local.getResults().getPoi(0).point;
        map.centerAndZoom(pp, 18);
    }
    $("#okAndCopy").click(function () {
        $("#labLng").val($("#lng").val());
        $("#labLat").val($("#lat").val());
    });
    $("#Lsearch").click(function () {
        map.removeOverlay(marker);
        if ($("#keyword").val().replace("", "") != "") {



            var local = new BMap.LocalSearch(map, {
                onSearchComplete: function () {
                    point = local.getResults().getPoi(0).point;
                    map.centerAndZoom(point, 12);

                    marker = new BMap.Marker(point);
                    map.addOverlay(marker);
                    $("#lng").val(point.lng);
                    $("#lat").val(point.lat);
                }
            });
            local.search($("#keyword").val());
        } else {
            point = new BMap.Point(parseInt($("#lng").val()), parseInt($("#lat").val()));

            map.centerAndZoom(point, 12);
            marker = new BMap.Marker(point);
            map.addOverlay(marker);
            $("#lng").val(point.lng);
            $("#lat").val(point.lat);
        }

    });



}